package mc.promcteam.engine.utils;

import java.text.DecimalFormat;

import org.jetbrains.annotations.NotNull;

public class NumberUT {

	private static final DecimalFormat NUMBER_FORMAT;
	
	static {
		NUMBER_FORMAT = new DecimalFormat("#.##"); // #.00 for 3.00 values
	}
	
	@NotNull
	public static String format(double d) {
		return NUMBER_FORMAT.format(d).replace(",", ".");
	}
	
	@NotNull
	public static String format(double d, @NotNull String pattern) {
		DecimalFormat format = new DecimalFormat(pattern);
		return format.format(d).replace(",", ".");
	}

	public static double round(double d) {
		return Double.valueOf(format(d)).doubleValue();
	}
	
	public static double round(double d, @NotNull String pattern) {
		return Double.valueOf(format(d, pattern)).doubleValue();
	}
	
	@NotNull
	public static String toRoman(int input) {
	    if (input < 1 || input > 3999) {
	        return "N/A";
	    }
	    
	    String s = "";
	    while (input >= 1000) {
	        s += "M";
	        input -= 1000;        
	    }
	    while (input >= 900) {
	        s += "CM";
	        input -= 900;
	    }
	    while (input >= 500) {
	        s += "D";
	        input -= 500;
	    }
	    while (input >= 400) {
	        s += "CD";
	        input -= 400;
	    }
	    while (input >= 100) {
	        s += "C";
	        input -= 100;
	    }
	    while (input >= 90) {
	        s += "XC";
	        input -= 90;
	    }
	    while (input >= 50) {
	        s += "L";
	        input -= 50;
	    }
	    while (input >= 40) {
	        s += "XL";
	        input -= 40;
	    }
	    while (input >= 10) {
	        s += "X";
	        input -= 10;
	    }
	    while (input >= 9) {
	        s += "IX";
	        input -= 9;
	    }
	    while (input >= 5) {
	        s += "V";
	        input -= 5;
	    }
	    while (input >= 4) {
	        s += "IV";
	        input -= 4;
	    }
	    while (input >= 1) {
	        s += "I";
	        input -= 1;
	    }    
	    return s;
	}
	
	public static int fromRoman(@NotNull String romanNumber) {
        int decimal = 0;
        int lastNumber = 0;
        String romanNumeral = romanNumber.toUpperCase();
        for (int x = romanNumeral.length() - 1; x >= 0 ; x--) {
            char convertToDecimal = romanNumeral.charAt(x);

            switch (convertToDecimal) {
                case 'M':
                    decimal = processDecimal(1000, lastNumber, decimal);
                    lastNumber = 1000;
                    break;

                case 'D':
                    decimal = processDecimal(500, lastNumber, decimal);
                    lastNumber = 500;
                    break;

                case 'C':
                    decimal = processDecimal(100, lastNumber, decimal);
                    lastNumber = 100;
                    break;

                case 'L':
                    decimal = processDecimal(50, lastNumber, decimal);
                    lastNumber = 50;
                    break;

                case 'X':
                    decimal = processDecimal(10, lastNumber, decimal);
                    lastNumber = 10;
                    break;

                case 'V':
                    decimal = processDecimal(5, lastNumber, decimal);
                    lastNumber = 5;
                    break;

                case 'I':
                    decimal = processDecimal(1, lastNumber, decimal);
                    lastNumber = 1;
                    break;
            }
        }
        return decimal;
    }
	
    private static int processDecimal(int decimal, int lastNumber, int lastDecimal) {
        if (lastNumber > decimal) {
            return lastDecimal - decimal;
        } 
        else {
            return lastDecimal + decimal;
        }
    }
    
	public static int[] splitIntoParts(int whole, int parts) {
	    int[] arr = new int[parts];
	    int remain = whole;
	    int partsLeft = parts;
	    for (int i = 0; partsLeft > 0; i++) {
	        int size = (remain + partsLeft - 1) / partsLeft; // rounded up, aka ceiling
	        arr[i] = size;
	        remain -= size;
	        partsLeft--;
	    }
	    return arr;
	}
}
